package cn.ll.action;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.web.servlet.ModelAndView;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import cn.ll.ModelAndView.ExcelView;
import cn.ll.po.Connector;
import cn.ll.po.Customer;
import cn.ll.po.Permissions;
import cn.ll.po.Regions;
import cn.ll.po.User;
import cn.ll.service.ConnectorService;
import cn.ll.service.ContractService;
import cn.ll.service.CustomerService;
import cn.ll.service.RegionsService;
import cn.ll.service.TaskService;
import cn.ll.util.SystermUtil;
import cn.ll.vo.LoginUser;

public class RegionsAction extends Action {

	RegionsService service = null;

	public void read(HttpServletRequest request, HttpServletResponse response) throws IllegalAccessException,
			InvocationTargetException, NoSuchMethodException {

		String customerid = request.getParameter("customerid");
		String id = request.getParameter("id");
		String by = request.getParameter("by");
		
		Regions regions = new Regions();
		
		BeanUtils.populate(regions, request.getParameterMap());
		
		List ls = service.read(regions);
		
		Map rs = new HashMap();
		rs.put("success", true);
		rs.put("items", ls);
		rtn(rs, request, response);

	}

	/**
	 * 根据状态获得用户列表
	 * 
	 * @param request
	 * @param response
	 */
	public void update(HttpServletRequest request, HttpServletResponse response) {

	}

	public void create(HttpServletRequest request, HttpServletResponse response) throws IllegalAccessException, InvocationTargetException {
		
		
		String[][] citys ={ 
				{"Africa","Egypt","Hurghada"},
				{"Africa","South Africa","Gauteng","Gape Town"},
				{"Africa","Nigeria","Lagos"},																						
				{"Asia","INDIA","Tamil","Nadu","Mumbai"},																							
				{"Asia","Kingdom of Saudi Arabia","Riyadh","Riyadh","Khobar"},																					
				{"Asia","Kuwait","Kuwait"},															
				{"Asia","Oman","SEEB"},																								
				{"Asia","Singapore","Singapore"},																								
				{"Asia","Turkey","Istanbul"},																								
				{"Asia","United Arab Emirrates","Abu Dhabi","Dubai","Sharjah","Ajman"},																					
				{"Asia","中国","上海","北京","香港","台北","天津","重庆","大连","长春","哈尔滨","南京","杭州","宁波","厦门","济南","青岛","武汉","广州","深圳","成都","西安","苏州","郑州","海口","三亚	","贵阳"},
				{"Asia","Japan","Tokyo","OSAKA","Nagoya","Yokohama","Kobe"},																				
				{"Asia","Korea","Seoul","Pusan","Taegu"},
				{"Europe","France","Paris","Lyon","Marseille","Lille","Toulouse	Nice","Nantes","Bordeaux"},																	
				{"Europe","Germany","Berlin","Hamburg","Munchen","Frankfurt","Cologne"},																	
				{"Europe","IRELAND","Dublin"},																							
				{"Europe","Italy","Milan","Rome"},																					
				{"Europe","United Kingdom","London","Birmingham	Liverpool","Leeds","Sheffield","Bristol","Manchester","Leicester","Coventry","worcestershire"},												
				{"North America","Canada","Montreal","Toronto","Ottawa","Quebec City","Vancouver"},																				
				{"North America","U.S.A	","New York-No","Los Angeles","Chicago","Washington","San Francisco","Dallas","Houston","St. Louis","Kansas City","Detroit","Phoenix-Mesa","Atlanta","San Diego","Cleveland-Akron","Irvine"},
				{"Europe","Austria","Vienna"},			
				{"Oceania","Australia","Sydney","Melbourne","Perth","Canberra"},
				{"Oceania","New Zealand","Auckland","North Shore"}		
		};
		int len1 = citys.length;
		for(int i = 0; i < len1; i++){
			String[] item = citys[i];
			int len2 = item.length;
			for(int j = 0; j < len2; j++){
				if(j == 0){//洲
					String continent = item[j];
					List<Regions> list  = service.findByRegionname(continent);
					if(list.size() == 0){
						Regions	regions = new Regions();
						regions.setLevel(1);
						regions.setRegionname(continent);
						service.create(regions);
						regions.setParentid(regions.getId());
						regions.setRootid(regions.getId());
						service.update(regions);
					}
				}
				else if(j == 1){//国家
					String continent = item[0];
					String country = item[j];
					List<Regions> list  = service.findByRegionname(continent);
					if(list.size() > 0){
						Regions	continent_regions = list.get(0);
						//查看该国家是否存在
						List<Regions> country_list  = service.findByRegionname(country);
						if(country_list.size() == 0){//不存在则插入
							Regions	country_regions = new Regions();
							country_regions.setLevel(2);
							country_regions.setRegionname(country);
							country_regions.setRootid(continent_regions.getId());
							country_regions.setParentid(continent_regions.getId());
							service.create(country_regions);
						}
					}
				}
				else{//城市
					String continent = item[0];
					String country = item[1];
					String city = item[j];
					List<Regions> list  = service.findByRegionname(continent);
					if(list.size() > 0){
						Regions	continent_regions = list.get(0);
						//查看该国家是否存在
						List<Regions> country_list  = service.findByRegionname(country);
						if(country_list.size() >= 0){//不存在则插入
							Regions	country_regions = country_list.get(0);
							List<Regions> city_list = service.findByRegionname(city);
							if(city_list.size() == 0){
								Regions	cityRegions = new Regions();
								cityRegions.setLevel(3);
								cityRegions.setRegionname(city);
								cityRegions.setParentid(country_regions.getId());
								cityRegions.setRootid(continent_regions.getId());
								service.create(cityRegions);
							}
						}
					
				}
			}
		}
		}
	}

	public RegionsService getService() {
		return service;
	}

	public void setService(RegionsService service) {
		this.service = service;
	}

}
